package com.momo.demo08_sort;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;
import org.junit.Test;

import com.momo.javabean.Article;
import com.momo.utils.DocumentUtils;
import com.momo.utils.LuceneUtils;

/**
 * @see 1����ͬ�Ľṹ����ͬ�Ĺؼ��ʣ��÷�һ��
 * @see 2����ͬ�Ľṹ����ͬ�Ĺؼ��ʣ��÷ֲ�һ����һ������£����ĸ���Ӣ��
 * @see 3����ͬ�Ľṹ����ͬ�Ĺؼ���,�ؼ��ʵĵ÷�Խ�ߣ�����Խ��ǰ.�ؼ��ʳ��ֵ�Ƶ��Խ�ߣ��÷�Խ��
 * @see 4��lucene�ľ�������
 */
public class SortTest {
    @Test
    public void testSearchIndex() throws Exception {
        IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
        QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, new String[]{"title", "content"}, LuceneUtils.analyzer);
        Query query = queryParser.parse("NBA");
        TopDocs topDocs = indexSearcher.search(query, 28);
        int count = topDocs.totalHits;// �ܵļ�¼��
        System.err.println("============================������������" + count);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        List<Article> articleList = new ArrayList<Article>();
        for (int i = 0; i < scoreDocs.length; i++) {
            int index = scoreDocs[i].doc;
            float score = scoreDocs[i].score;// ��ضȵ÷�
            System.err.println("======================��ǰ�ؼ���ƥ��ȣ�" + score);
            Document document = indexSearcher.doc(index);
            Article article = DocumentUtils.document2Article(document);
            articleList.add(article);
        }

        // �����ѯ��������Ϣ
        for (Article article : articleList) {
            System.out.println(article.getId());
            System.out.println(article.getTitle());
            System.out.println(article.getContent());
        }
    }
}
